package nju.bishe.weibo;

import java.util.List;

import nju.bishe.weibo.data.UserToken;
import nju.bishe.weibo.data.db.UserTokenTable;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

/**
 * 
 * @author longwei.wu
 * @version $Id: DelAccountActivity.java, v 0.1 2011-3-27 下午01:16:03 longwei.wu
 *          Exp $
 */
public class DelAccountActivity extends Activity {

	private Spinner cuisine;
	private Button delButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.del_account_layout);
		cuisine = (Spinner) findViewById(R.id.cuisine);
		final String[] ids = getUserIdFromDB();
		String[] names = getUserNameFromDB();

		ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this,
				R.layout.spinner_view, names);
		cuisines.setDropDownViewResource(R.layout.spinner_view_dropdown);
		this.cuisine.setAdapter(cuisines);

		delButton = (Button) findViewById(R.id.del_button);
		delButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Dialog dialog = new AlertDialog.Builder(DelAccountActivity.this)
						.setTitle("警告")
						.setMessage("你确定删除吗？")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {

										delAccount(ids[cuisine.getSelectedItemPosition()]);
										//delAccount((String) cuisine
											//	.getSelectedItem());
										String[] idsTemp = getUserIdFromDB();
										if (idsTemp.length == 0) {
											Intent intent = new Intent(
													DelAccountActivity.this,
													FirstLoginActivity.class);
											intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
											startActivity(intent);
										} else {
											Intent intent = new Intent(
													DelAccountActivity.this,
													LoginActivity.class);
											intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
											startActivity(intent);
										}
									}
								})
						.setNeutralButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										dialog.dismiss();
									}
								}).create();// 创建按钮

				// 显示对话框
				dialog.show();
			}

		});

	}

	private String[] getUserIdFromDB() {

		final UserTokenTable dbHelper = new UserTokenTable(this);
		List<UserToken> users = dbHelper.getAllUsersRemember();
		String[] ids = new String[users.size()];
		for (int i = 0; i < users.size(); i++) {
			ids[i] = users.get(i).getUser_id();
			System.out.println(ids[i]);
		}
		return ids;
	}

	private String[] getUserNameFromDB() {
		final UserTokenTable dbHelper = new UserTokenTable(this);
		List<UserToken> users = dbHelper.getAllUsersRemember();
		String[] names = new String[users.size()];
		for (int i = 0; i < users.size(); i++) {
			names[i] = users.get(i).getUserName();
			System.out.println(names[i]);
		}
		return names;
	}

	private void delAccount(String id) {
		final UserTokenTable dbHelper = new UserTokenTable(this);
		dbHelper.delete(Long.parseLong(id));
	}
}
